<?php

namespace Tbf\Bundle\RefactorBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

/**
 * 根据某个规则大批量替换字符串
 */
class BulkReplaceCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('tbf:refactor:bulk-replace-command')
            ->setDescription('bulk replace command');
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {

    }

    protected function replaceOne($from_string,$to_string,$base_dir)
    {
        $finder = Finder::create()->contains($from_string)->in($this->$base_dir);
        foreach ($finder as $file) {
            /** @var \Symfony\Component\Finder\SplFileInfo $file */
            $content = file_get_contents($file);
            $content = str_replace($from_string,$to_string,$content);
            file_put_contents($file,$content);
        }
    }

}
